package easy;

public class a704 {
    /**
     * 实际使用求中间mid索引建议用这种方法：int mid = left + (right-left)/2;
     * 可以防止left+right溢出（超出整数范围）。
     */

    class Solution {
        public int search(int[] nums, int target) {
            int left = 0;
            int right = nums.length - 1;
            while (left <= right) {
                int mid = left + (right - left) / 2;
                if (nums[mid] == target) return mid;
                else if (nums[mid] > target) right = mid - 1;
                else left = mid + 1;
            }
            return -1;
        }
    }
}
